#include "poweron.h"

void powerOnCheck(void)
{
    /*
        1.检查是否校准
        2.检查细分设置
        3.检查闭环/开环
        4.#检查脉冲计数方向
        5.检查驱动使能状态
    */
    // 获取初始位置
    encoder_cnt = CaliTab.get(Encoder.ReadAngleHwCsYORO());
    //校正拨码开关ON进入编码器芯片校正程序
    if (OperationalMode.CALI == LOW)
    {
        DEBUG_port->printf("Enter calibration mode\r\n");
        EncoderCalibrating();
    }
    //电子齿轮设置
    if ((OperationalMode.SET1 == HIGH) && (OperationalMode.SET2 == HIGH))
    {
        DEBUG_port->printf("Microstep: 4\r\n");
        //电机跑一圈需要的控制脉冲数为800
        stepangle = 2048;
    }
    else if ((OperationalMode.SET1 == LOW) && (OperationalMode.SET2 == HIGH))
    {
        DEBUG_port->printf("Microstep: 8\r\n");
        //电机跑一圈需要的控制脉冲数为1600
        stepangle = 1024;
    }
    else if ((OperationalMode.SET1 == HIGH) && (OperationalMode.SET2 == LOW))
    {
        DEBUG_port->printf("Microstep: 16\r\n");
        //电机跑一圈需要的控制脉冲数为3200
        stepangle = 512;
    }
    else
    {
        DEBUG_port->printf("Microstep: 32\r\n");
        //电机跑一圈需要的控制脉冲数为6400
        stepangle = 256;
    }
    if (OperationalMode.OPCL == LOW)
    {
        DEBUG_port->printf("Closed loop mode\r\n");
        closedloop_mode = ENABLE;
    }
    else
    {
        DEBUG_port->printf("Open loop mode\r\n");
        closedloop_mode = DISABLE;
    }
    if (StepperInput.IsEnlable())
    {
        encoder_cnt = CaliTab.get(Encoder.ReadAngleHwCsYORO());
        step_input_offset = encoder_cnt;
        motor_pos_set = encoder_cnt;
        motor_pos_set_last = motor_pos_set;
        encoder_cnt_last = encoder_cnt;
        motor_pos = encoder_cnt;
        motor_pos_last = motor_pos;
        motor_rev_cnt = 0;
    }
    else
    {
        // 电桥电流置0
        CoilA.free();
        CoilB.free();
    }
}
